// This File is part of the "nyaqt" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using nyaqt.Plugins;
using System.Globalization;
using Mono.Data.SybaseClient;
using System.Text.RegularExpressions;
using System.Data.Common;

namespace nyaqt.MonoSybasePlugin
{
    class ConnectionProperties : SybaseConnectionProperties, nyaqt.Plugins.IConnectionProperties
    {
        static ConnectionProperties()
        {
            //strings = new int[] { SybaseType.Text,SybaseType.nt SybaseType.Char, SybaseType.NChar,  SybaseType.NText, SybaseType.NVarChar, SybaseType.VarChar, SybaseType.VarBinary };
            //dates = new int[] { SybaseType.DateTime, SybaseType.SmallDateTime };
            //numfixed = new int[] { SybaseType.Text, SybaseType.NText, SybaseType.BigInt, SybaseType.Float, SybaseType.Int, SybaseType.Money, SybaseType.Real, SybaseType.SmallInt, SybaseType.SmallMoney, SybaseType.TinyInt };
            //numvar = new int[] { SybaseType.Decimal };
        }

        SybaseConnection SybaseConnection
        {
            get
            {
                return _cn as SybaseConnection;
            }
        }

        public System.Data.IDbConnection Connection
        {
            get
            {
                return this.SybaseConnection;
            }
            set
            {
                if (this.SybaseConnection != null)
                    this.SybaseConnection.InfoMessage -= new SybaseInfoMessageEventHandler(Connnection_InfoMessage);

                _cn = value as SybaseConnection;
                if (value != null)
                {
                    this.SybaseConnection.InfoMessage += new SybaseInfoMessageEventHandler(Connnection_InfoMessage);
                }
            }
        }

        void Connnection_InfoMessage(object sender, SybaseInfoMessageEventArgs e)
        {
            foreach (SybaseError err in e.Errors)
            {
                FireConnectionMessage(err.Message);
            }
        }

        public string Server
        {
            get
            {
                return this.SybaseConnection.DataSource;
            }
        }

        public string ServerBrowseKey
        {
            get
            {
                return Server + "/" + UserName;
            }
        }

        override protected List<DatabaseEntityInfo> GetDatabaseTablesEx(string database, string type)
        {
            List<DatabaseEntityInfo> ret = new List<DatabaseEntityInfo>();
            try
            {
                ret = base.GetDatabaseTablesEx(database, type);
            }
            catch (SybaseException ex)
            {
                foreach (SybaseError err in ex.Errors)
                {
                    if (err.Number == 10351)
                    {
                        DatabaseEntityInfo e = new DatabaseEntityInfo();
                        e.DisplayName = ex.Message;
                        e.TableType = "ERROR";
                        ret.Add(e);
                        return ret;
                    }
                }
            }
            return ret;
        }

        override public List<DatabaseEntityInfo> GetDatabaseStoredProcedures(string database)
        {
            List<DatabaseEntityInfo> ret = new List<DatabaseEntityInfo>();
            try
            {
                ret = base.GetDatabaseStoredProcedures(database);
            }
            catch (SybaseException ex)
            {
                foreach (SybaseError err in ex.Errors)
                {
                    if (err.Number == 10351)
                    {
                        DatabaseEntityInfo e = new DatabaseEntityInfo();
                        e.DisplayName = ex.Message;
                        e.TableType = "ERROR";
                        ret.Add(e);
                        return ret;
                    }
                }
            }
            return ret;
        }

        override public List<DatabaseEntityInfo> GetDatabaseTableColumns(string database, string tableNamespace, string table)
        {
            List<DatabaseEntityInfo> ret = new List<DatabaseEntityInfo>();
            try
            {
                ret = base.GetDatabaseTableColumns(database, tableNamespace, table);
            }
            catch (SybaseException ex)
            {
                foreach (SybaseError err in ex.Errors)
                {
                    if (err.Number == 10351)
                    {
                        DatabaseEntityInfo e = new DatabaseEntityInfo();
                        e.DisplayName = ex.Message;
                        e.TableType = "ERROR";
                        ret.Add(e);
                        return ret;
                    }
                }
            }
            return ret;
        }

        override protected void GetDatabaseTableRelations(List<DatabaseEntityInfo> list, string database, string tableNamespace, string table, bool outgoing)
        {
            try
            {
                base.GetDatabaseTableRelations(list, database, tableNamespace, table, outgoing);
            }
            catch (SybaseException ex)
            {
                foreach (SybaseError err in ex.Errors)
                {
                    if (err.Number == 10351)
                    {
                        DatabaseEntityInfo e = new DatabaseEntityInfo();
                        e.DisplayName = ex.Message;
                        e.TableType = "ERROR";
                        list.Add(e);
                        break;
                    }
                }
            }
        }

        public List<DatabaseEntityInfo> GetDatabaseTableRelationColumns(string database, string tableNamespace, string table, string relatedTableNamespace, string relatedTable)
        {
            List<DatabaseEntityInfo> ret = new List<DatabaseEntityInfo>();
            try
            {
                ret = base.GetDatabaseTableRelationColumns(database, tableNamespace, table, relatedTableNamespace, relatedTable);
            }
            catch (SybaseException ex)
            {
                foreach (SybaseError err in ex.Errors)
                {
                    if (err.Number == 10351)
                    {
                        DatabaseEntityInfo e = new DatabaseEntityInfo();
                        e.DisplayName = ex.Message;
                        e.TableType = "ERROR";
                        ret.Add(e);
                        break;
                    }
                }
            }
            return ret;
        }

        override public List<DatabaseEntityInfo> GetDatabaseStoredProcedureParameters(string database, string tableNamespace, string table)
        {
            List<DatabaseEntityInfo> ret = new List<DatabaseEntityInfo>();
            try
            {
                ret = base.GetDatabaseStoredProcedureParameters(database, tableNamespace, table);
            }
            catch (SybaseException ex)
            {
                foreach (SybaseError err in ex.Errors)
                {
                    if (err.Number == 10351)
                    {
                        DatabaseEntityInfo e = new DatabaseEntityInfo();
                        e.DisplayName = ex.Message;
                        e.TableType = "ERROR";
                        ret.Add(e);
                        break;
                    }
                }
            }
            return ret;
        }

        public string FormatOwnException(int lineoffset, Exception ex)
        {
            string msg = "";
            if (ex is SybaseException)
            {
                SybaseException ex2 = ex as SybaseException;
                foreach (SybaseError err in ex2.Errors)
                {
                    if (msg != "")
                        msg += "\r\n";

                    int line = err.LineNumber;
                    //if (line > 1) line -= 1;
                    msg += "Line:" + (lineoffset + line) + " Number:" + err.Number + " State:" + err.State;
                    if (!string.IsNullOrEmpty(err.Procedure))
                        msg += "Procedure: " + err.Procedure;
                    msg += " ; " + err.Message;
                }
            }
            return msg;
        }

        protected override void GetScaleAndPrecision(out int scale, out int precision, IDataParameter param)
        {
            SybaseParameter p = param as SybaseParameter;
            if (p.Scale == 0)
                base.GetScaleAndPrecision(out scale, out precision, param);
            else
            {
                scale = p.Scale;
                precision = p.Precision;
            }
        }

    }
}
































